"""使用loguru的日志配置"""
from loguru import logger
from pathlib import Path
import sys
from src.config.settings import get_settings

# 移除默认的控制台处理器
logger.remove()

# 创建日志目录
log_dir = Path("logs")
log_dir.mkdir(exist_ok=True)

# 获取配置
config = get_settings()

# 添加控制台输出
logger.add(
    sys.stdout,
    format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>",
    level=config.LOG_LEVEL,
    colorize=True
)

# 添加文件输出 - 应用日志
logger.add(
    log_dir / "app.log",
    format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}",
    level=config.LOG_LEVEL,
    rotation="10 MB",
    retention="30 days",
    compression="zip",
    encoding="utf-8"
)

# 添加错误日志文件
logger.add(
    log_dir / "error.log",
    format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}",
    level="ERROR",
    rotation="10 MB",
    retention="30 days",
    compression="zip",
    encoding="utf-8"
)

# 导出logger实例
app_logger = logger
